<template>
    <div class="major-add">
        <el-form label-width="80px" ref="form" :model="form" :rules="rules">
            <el-form-item label="班级名" prop="name">
                <el-input v-model="form.name" placeholder="请输入班级名" />
            </el-form-item>

            <el-form-item label="所属专业" prop="majorId">
                <el-select filterable v-model="form.majorId" placeholder="请选择专业">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="年级" prop="grade">
                <el-input-number v-model="form.grade" controls-position="right" @change="handleChange" :min="1000"
                    :max="3000"></el-input-number>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer" style="display: flex; justify-content: flex-end;">
            <el-button type="primary" @click="onsubmit">确定</el-button>
            <el-button type="primary" @click="handclose" plain>取消</el-button>
        </span>
    </div>
</template>
<script>
import { majornames, adminclass } from '@/api/class';
export default {
    data() {
        return {
            form: { name: '', majorId: "", id: -1, grade: "2017" },
            options: [{
                value: '',
                label: ''
            }],
            rules: {
                name: [
                    { required: true, message: '请输入班级名', trigger: 'blur' }
                ],
                majorId: [
                    { required: true, message: '请选择专业', trigger: 'change' }
                ]
            }
        }
    },
    mounted() {
        majornames().then(res => {
            console.log(res);
            this.options = (res.data || []).map(item => ({
                label: item.name,
                value: item.id
            }));
        })
    },
    methods: {
        async onsubmit() {
            this.$refs["form"].validate(async flag => {
                if (flag) {
                    console.log(this.form)
                    const res = await adminclass(this.form);
                    if (res.code == 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                        });
                        this.$emit('done');
                    } else {
                        console.log(res);
                        this.$message({
                            message: '操作失败',
                            type: 'error',
                        });
                    }
                }
            })
        },
        handclose() {
            this.$emit('close')
        },
        handleChange(value) {
            console.log(value);
        }
    }
}
</script>
